package com.wmtiger.component
{
	import com.wmtiger.component.base.WMSprite;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	public class WMImage extends WMSprite
	{
		private var _url:String = "";
		private var _ldr:Loader;
		
		public function WMImage(url:String = "")
		{
			_url = url;
			super();
			initView();
		}
		
		protected function initView():void
		{
			if (_url.length > 0)
			{
				initLoad();
			}
		}
		
		private function initLoad():void
		{
			if (_ldr == null)
			{
				_ldr = new Loader();
				addLdrEvt();
				_ldr.load(new URLRequest(_url));
			}
		}
		
		public function load(url:String):void
		{
			_url = url;
			initLoad();
		}
		
		protected function addLdrEvt():void
		{
			_ldr.contentLoaderInfo.addEventListener(Event.INIT, onInit);
			_ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
		}
		
		protected function removeLdrEvt():void
		{
			_ldr.contentLoaderInfo.removeEventListener(Event.INIT, onInit);
			_ldr.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_ldr.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
		}
		
		protected function onInit(event:Event):void
		{
			removeLdrEvt();
		}
		
		protected function onIOError(event:IOErrorEvent):void
		{
			removeLdrEvt();
		}
		
		protected function onProgress(event:ProgressEvent):void
		{
			
		}
		
		public override function dispose():void
		{
			if (_ldr)
			{
				_ldr.unloadAndStop();
			}
			_ldr = null;
		}
	}
}